package com.xhwl.centiotdata.handler.info.impl;

import com.xhwl.centiotdata.handler.info.InfoDeviceFactory;
import com.xhwl.centiotdata.handler.info.InfoDeviceHandler;
import com.xhwl.centiotdata.pojo.constant.DaGuanInfoConstant;
import com.xhwl.centiotdata.pojo.dto.dahua.DeviceDaGuanDTO;
import com.xhwl.centiotdata.pojo.dto.haikanginfo.InfoIntegrationPropertiesDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.suppliersystem.daguan.DeviceDaGuanHandler;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.enums.device.DeviceTypeEnum;
import com.xhwl.common.pojo.cent.device.Device;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tangwei
 * @description 达观信息发布屏设备
 * @create 2023-10-27 10:07
 */
@Slf4j
@Service
public class DeviceIntegrationGaGuanHandler extends InfoDeviceHandler {

    @Autowired
    private IDeviceCentService deviceCentService;
    @Autowired
    private DeviceDaGuanHandler deviceDaGuanHandler;

    @Override
    public void afterPropertiesSet() throws Exception {
        InfoDeviceFactory.register(ProductEnum.DAGUAN_INFO.getId(), this);
    }

    @Override
    public void pullInfoDevice(InfoIntegrationPropertiesDTO configDTO) {
        // 拉取数据
        List<DeviceDaGuanDTO> deviceList = deviceDaGuanHandler.pullDevice(configDTO);
        if (CollectionUtils.isEmpty(deviceList)) {
            return;
        }
        // 转换数据
        List<DeviceDTO> deviceDTOList = this.handleDevice(configDTO, deviceList);
        // 存储业务中台
        deviceCentService.asyncUpdateDevice(deviceDTOList);
    }

    @Override
    public void pullDeviceOnlineStatus(InfoIntegrationPropertiesDTO configDTO) {
        // 拉取数据
        List<DeviceDaGuanDTO> deviceList = deviceDaGuanHandler.pullDevice(configDTO);
        if (CollectionUtils.isEmpty(deviceList)) {
            return;
        }
        // 转换数据
        List<Device> uodateList = this.handleDeviceOnlineStatus(configDTO, deviceList);
        // 更新数据
        deviceCentService.asyncUpdateOnlineStatus(uodateList);
    }

    /**
     * 处理设备的在离线状态
     *
     * @param configDTO           集成配置
     * @param deviceDaGuanDTOList 达观设备列表
     * @return result
     */
    private List<Device> handleDeviceOnlineStatus(InfoIntegrationPropertiesDTO configDTO, List<DeviceDaGuanDTO> deviceDaGuanDTOList) {
        List<Device> deviceList = new ArrayList<>(deviceDaGuanDTOList.size());
        deviceDaGuanDTOList.forEach(item -> {
            Device device = new Device();
            device.setProductId(configDTO.getProjectId());
            device.setThirdDeviceId(item.getDeviceCode());
            device.setDeviceTypeId(DaGuanInfoConstant.DEVICE_TYPE);
            device.setOnlineStatus(item.getIsOnline() ? DeviceOnlineStatusEnum.ONLINE : DeviceOnlineStatusEnum.OFFLINE);
            deviceList.add(device);
        });
        return deviceList;
    }

    /**
     * 处理设备的业务数据
     *
     * @param configDTO  集成配置
     * @param deviceList 达观设备列表
     * @return result
     */
    private List<DeviceDTO> handleDevice(InfoIntegrationPropertiesDTO configDTO, List<DeviceDaGuanDTO> deviceList) {
        List<DeviceDTO> deviceDTOList = new ArrayList<>(deviceList.size());
        deviceList.forEach(device -> {
            DeviceDTO deviceDTO = new DeviceDTO();
            deviceDTO.setProjectId(configDTO.getProjectId());
            deviceDTO.setProductId(configDTO.getProductId());
            deviceDTO.setCategoryId(configDTO.getCategoryEnum().getId());
            deviceDTO.setDeviceTypeId(DeviceTypeEnum.INFO_SYSTEM.getCode());
            deviceDTO.setName(device.getDeviceCode());
            deviceDTO.setThirdDeviceId(device.getDeviceName());
            deviceDTO.setOnlineStatus(device.getIsOnline() ? DeviceOnlineStatusEnum.ONLINE.id : DeviceOnlineStatusEnum.OFFLINE.id);
            deviceDTOList.add(deviceDTO);
        });
        return deviceDTOList;
    }

}
